package com.rays.sysmech.core;

import java.io.File;

public class DuplicateFileProcessor 
{
	public static DuplicateDataInfo FindAllDuplicates(File file)
	{
		DuplicateDataInfo duplicateData = null;
		if(null != file)
		{
			duplicateData = new DuplicateDataInfo();
			DataRecords records = new DataRecords();
			findDuplicates(file, records, duplicateData);
		}
		return duplicateData;
	}
	
	private static void findDuplicates(File iFile, DataRecords records,
			DuplicateDataInfo duplicateVsOriginal)
	{
		File[] contents = iFile.listFiles();
		if(null != contents)
		{
			for(File currentFile : contents)
			{
				if(currentFile.isDirectory())
				{
					findDuplicates(currentFile, records, duplicateVsOriginal);
				}
				else
				{
					FileData filedata = new FileData(currentFile); 
					if(records.contains(filedata))
					{
						int containedAt = records.indexOf(filedata);
						FileData originalFile = records.get(containedAt);
						duplicateVsOriginal.addData(originalFile, filedata);
					}
					else
					{
						records.add(filedata);
					}
				}
			}
		}
	}

}
